package com.yanftch.stopcoding.test;

import com.yanftch.stopcoding.bean.News;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeiboHotSearchCrawler {
    private static String url = "https://s.weibo.com/top/summary?cate=realtimehot";
    private static String get_tdid = "https://passport.weibo.com/visitor/genvisitor?cb=gen_callback";

    public static void main(String[] args) {
        try {
            getHot();
            grabBaiduHotNewsJson();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void grabBaiduHotNewsJson() {
        String url = "https://top.baidu.com/board?tab=realtime&sa=fyb_realtime_31065";
        List<News> list = new ArrayList<>();
        try {
            Document doc = Jsoup.connect(url).get();
            //标题
            Elements titles = doc.select(".c-single-text-ellipsis");
            //图片
            Elements imgs = doc.select(".category-wrap_iQLoo .index_1Ew5p").next("img");
            //内容
            Elements contents = doc.select(".hot-desc_1m_jR.large_nSuFU");
            //推荐图
            Elements urls = doc.select(".category-wrap_iQLoo a.img-wrapper_29V76");
            //热搜指数
            Elements levels = doc.select(".hot-index_1Bl1a");
            for (int i = 0; i < levels.size(); i++) {
                News o = new News();
                o.setTitle(titles.get(i).text().trim());
                o.setImg(imgs.get(i).attr("src"));
                o.setContent(contents.get(i).text().replaceAll("查看更多>", "").trim());
                o.setUrl(urls.get(i).attr("href"));
                o.setLevel(levels.get(i).text().trim());
                list.add(o);
            }

            for (News news : list) {
                System.out.println("百度热搜：" + news.getTitle() + "，热搜指数：" + news.getLevel() + "，内容：" + news.getContent() + "，图片：" + news.getImg() + "，链接：" + news.getUrl());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
    * 微博：https://juejin.cn/post/7221046247263322171
    */
    private static void getHot() {
        // 创建请求
        Map<String, String> hMap = new HashMap<>();
        hMap.put("Cookie", "SUB=_2AkMQUXTgf8NxqwFRmf4VzGnkaI1zzQHEieKmDYU7JRMxHRl-yT9kqkhStRB6O9FaD1cEwKM32NnxNptG8A2i-2KTP4T9" + ";SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWhnThkBXkehJIPX9Op.1Lh");
        Headers headers = Headers.Companion.of(hMap);
        Request request = new Request.Builder()
                .url(url)
                .get()
                .headers(headers)
                .build();

        OkHttpClient client = new OkHttpClient();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                System.out.println("日志HOT："+response.body().string());
                // window.gen_callback && gen_callback({"retcode":20000000,"msg":"succ","data":{"tid":"01Ab5o_NJk8VP9OJ7D7a599FFdKWrwZFOxk-0bjhAVksXE","new_tid":true}});
                // window.cross_domain && cross_domain({"retcode":20000000,"msg":"succ","data":{"sub":"_2AkMQUXTgf8NxqwFRmf4VzGnkaI1zzQHEieKmDYU7JRMxHRl-yT9kqkhStRB6O9FaD1cEwKM32NnxNptG8A2i-2KTP4T9","subp":"0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWhnThkBXkehJIPX9Op.1Lh"}});
                Document document = Jsoup.parse(response.body().string());
                Element item = document.getElementsByTag("tbody").first();
                if (item != null) {
                    Elements items = item.getElementsByTag("tr");
                    for (Element tmp : items) {
                        Element rankEle = tmp.getElementsByTag("td").first();
                        Elements textEle = tmp.select(".td-02").select("a");
                        Elements tagEle = tmp.select(".td-03");
                        Elements rdEle = tmp.select(".td-02").select("span");
                        String url = "https://s.weibo.com/" + textEle.attr("href");
                        System.out.println("textEle=" + textEle.text() + ", tagEle=" + tagEle.text() + ", rdEle=" + rdEle.text() + ", url=" + url);
                    }
                }
            }
        });
    }

    private static void getTdid() {
        // 创建请求
        Request request = new Request.Builder()
                .url(get_tdid)
                .get()
                .build();
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("日志：" + response.body().string());
                // window.gen_callback && gen_callback({"retcode":20000000,"msg":"succ","data":{"tid":"01Ab5o_NJk8VP9OJ7D7a599FFdKWrwZFOxk-0bjhAVksXE","new_tid":true}});
                // window.cross_domain && cross_domain({"retcode":20000000,"msg":"succ","data":{"sub":"_2AkMQUXTgf8NxqwFRmf4VzGnkaI1zzQHEieKmDYU7JRMxHRl-yT9kqkhStRB6O9FaD1cEwKM32NnxNptG8A2i-2KTP4T9","subp":"0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWhnThkBXkehJIPX9Op.1Lh"}});
            }
        });
    }
}